package com.xz.example.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author 张志航     
 * @version V1.0.0
 * @projectName design_mode
 * @title     LoadBalance   
 * @package    com.xz.example.model  
 * @date   2019/12/3 14:40  
 * @explain
 */
public class LoadBalance {

    private static LoadBalance loadBalance = null;

    private List<String> serverList = null;

    private LoadBalance(){
        serverList = new ArrayList();
    }

    public static LoadBalance getLoadBalance(){
        if (null == loadBalance){
            loadBalance = new LoadBalance();
        }
        return loadBalance;
    }

    public void addServer(String server){
        serverList.add(server);
    }

    public void removeServer(String server){
        serverList.remove(server);
    }

    // 随机获取服务器
    public String getServer(){
        Random random = new Random();
        int idx = random.nextInt(serverList.size());
        return serverList.get(idx);
    }

}
